package lib.pacificapp.widget;
 
import java.util.List;
 

import lib.pacificapp.widget.slideview.GetCellView;
import lib.pacificapp.widget.slideview.Slide;

 
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet; 
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout; 

public class SlideView  extends RelativeLayout implements OnPageChangeListener {
    
	private int errorImg;
	private int slideWidth;
	private int slideHeight;
    public int getSlideWidth() {
		return slideWidth;
	}


	public void setSlideWidth(int slideWidth) {
		this.slideWidth = slideWidth;
	}


	 
	public int getSlideHeight() {
		return slideHeight;
	}


	public void setSlideHeight(int slideHeight) {
		this.slideHeight = slideHeight;
	}



	private GetCellView  cellView;
    private GetCellView  getCellView(){
 	   if(cellView==null)
 		  cellView=new GetCellView(){
            @Override
				public LinearLayout getView(Slide model) {
         	   LinearLayout v=new LinearLayout(context);
         	      v.setLayoutParams(new LayoutParams(-1, -1));
         	    Img imageView = new Img(context);
         	        v.addView(imageView,new LayoutParams(-1, -1));
         	     if(!model.getImageUrl().equals("")&&getSlideWidth()>0)
   			         imageView.url(model.getImageUrl(),getSlideWidth(),getSlideHeight(),getErrorImg());
   			   return v;
				}
 	     };
 	   return cellView;
 	   
    }
   
    
 protected String getTipColor() {
		if(tipColor==null)
		   tipColor="#55c2c2c2";
		return tipColor;
	}
 protected void setTipColor(String tipColor) {
		this.tipColor = tipColor;
	}
	private Context context;
 private String tipColor;
 private String activeTipColor;
	



 protected String getActiveTipColor() {
		if(activeTipColor==null)
			activeTipColor="#ffffff";
		return activeTipColor;
	}

 protected void setActiveTipColor(String activeTipColor) {
		this.activeTipColor = activeTipColor;
	}

	public SlideView(Context context) {
		super(context);
		initView(context);
	}

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
 }

	public class ViewPager2 extends ViewPager{
		public ViewPager2(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
	 
		 
		 
		 
	    @Override  
	    public boolean onInterceptTouchEvent(MotionEvent arg0) {  
	        
	        isRun=false;
	        return super.onInterceptTouchEvent(arg0);  
	    }  
	  
	  
	 
	}
	
	public ViewPager2 target;
	private LinearLayout border;
 
	private void initView(Context cont) {
		context = cont;
	 
		if (target == null) {
			target = new ViewPager2(context);
			 //target.setScanScroll(false);
			 
			RelativeLayout.LayoutParams ps = new RelativeLayout.LayoutParams(
					-1, -1);
			target.setLayoutParams(ps);
			 addView(target);
		}
		if (border == null) {
			border = new LinearLayout(context);
			RelativeLayout.LayoutParams ps2 = new RelativeLayout.LayoutParams(
					-1, RelativeLayout.LayoutParams.WRAP_CONTENT);
			
			 ps2.setMargins(0, 0, 0, 10);
			ps2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			border.setLayoutParams(ps2);
			border.setGravity(Gravity.CENTER);
			border.setOrientation(LinearLayout.HORIZONTAL);
			 addView(border);
		}
	}
	public void hideTip(){
		border.setVisibility(View.GONE);
	}
	private class Tip extends View {
		         public String color="#b2b2b2";
			    public Tip(Context context) {  super(context); }
             public Tip(Context context, AttributeSet attrs) {   super(context, attrs);  }
            @Override
			    protected void onDraw(Canvas canvas) {
			        super.onDraw(canvas);
			        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
			        paint.setStyle(Paint.Style.FILL);
			        paint.setColor(Color.parseColor(color));
			     //   paint.setAlpha(60);
			        canvas.drawCircle(10, 10, 10, paint);
			 }

	}
	
 private Tip getTip(){
 	 
 	Tip r=new Tip(context); 
	     LinearLayout.LayoutParams ps=new LinearLayout.LayoutParams(20,20);
	      ps.setMargins(10, 0,10, 0);
	      r.setLayoutParams(ps);
	      r.color=getTipColor();
	    return r;
 }
 protected void SetCellView(GetCellView getCellview){
	   cellView=getCellview;
 }
 protected void clean(){
 	    cellviews=new  LinearLayout[0];
 	 
 		 target.setAdapter(new MyAdapter());
			 border.removeAllViews();
 	
 }
 private List<Slide> models;
 public void setModels(List<Slide> _models){  
		models=_models;
		int count=models.size();
		
		border.removeAllViews();
		cellviews=new LinearLayout[count]; 
		 
		for (int i = 0; i < count; i++) {
			Slide mod=models.get(i);
			 
			     mod.setIndex(i);
			     LinearLayout v=  getCellView().getView(mod);
			     v.setTag(mod);
   			 v.setOnClickListener(new OnClickListener(){
                    @Override
   				public void onClick(View arg0) {
                   	 Slide mod= (Slide)arg0.getTag();
                   	 if(mod.getOnSildeClickListener()!=null)
                   	    mod.getOnSildeClickListener().click(mod);
                   }
   			 });
			  cellviews[i]=v;
			  border.addView(getTip()); 
		}
		 
		 		// 璁剧疆Adapter
		target.setAdapter(new MyAdapter()); 
	    target.setOnPageChangeListener(this);
	    target.setCurrentItem(2000);

	}
	 
	public Handler handler;
	
	private  int i=0;
	private boolean ir=true;
	 public boolean isPlay=true;
	protected void show(int i){
		 target.setCurrentItem(i);
	}
	private Thread mThread;
	public synchronized void closeThread() {  
        try {  
            
            mThread.stop(); 
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
	public void play(final int time){
		 handler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
                   target.setCurrentItem(msg.arg1);
          }
        };
         if(mThread==null){
        mThread=new Thread(new Runnable() {
          @Override
          public void run() {
               while(isPlay) {
            	   if(isRun){
                 try {
             	  Message ms = handler.obtainMessage();
                            ms.arg1 = i;
                        handler.sendMessage(ms);
                        Thread.sleep(time);
                        if(i==cellviews.length-1){
                     	   ir=false;
                        }
                        if(i==0)
                     	   ir=true;
                        if(ir)
                           i++;
                        else
                     	   i--;
                        
                    } catch ( Exception e) {
                    }
                          }
                  }
         }
         
    });
        mThread.start();
         }else{
        	 this.isRun=true;
        	 
         }
       
        
	}
	private boolean isRun=true;
	
	protected void stop(){
		isRun=false;
	}
	
	private void setActiveTip(int pos){
		for(int i=0;i<border.getChildCount();i++){ 
			Tip r=(Tip)border.getChildAt(i);
			if(i==pos) {
				r.color=getActiveTipColor();

			      }
		     else {
		    	 r.color= getTipColor(); 
			}
			r.requestLayout();
		 }
	}

	 private LinearLayout[] cellviews;  
	private class MyAdapter extends PagerAdapter {

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			if(cellviews.length>0){
			   ((ViewPager) container).removeView(cellviews[position
					 % cellviews.length]);
			}
		}

		  @Override
		  public Object instantiateItem(View container, int position) {
			
			int ind=position % cellviews.length;
			 
			LinearLayout cell=cellviews[ind];
			 
			((ViewPager) container).removeView(cell);
			((ViewPager) container).addView(cell, 0);
			return cell;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return cellviews.length;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}
   
	@Override
	public void onPageSelected(int position) {
		   
		   setActiveTip(position % cellviews.length);
		   isRun=true;
	}


	public int getErrorImg() {
		return errorImg;
	}


	public void setErrorImg(int errorImg) {
		this.errorImg = errorImg;
	}

}
